package cn.lite3.soundPlayer.core
{
	import cn.lite3.soundPlayer.events.CoreEventDispatcher;
	import cn.lite3.soundPlayer.events.SoundTransformEvent;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author lite3
	 */
	public class SimpleSound implements ISimpleSound
	{
		private var _pos:Number;
		private var sound:Sound;
		
		private var st:SoundTransform;
		private var sc:SoundChannel;
		
		private var length:int;
		private var 
		
		public function SimpleSound() 
		{
			sound = new Sound();
			CoreEventDispatcher.dispatcher.addEventListener(SoundTransformEvent.CHANGE, stChangeHandler);
		}
		
		private function stChangeHandler(e:SoundTransformEvent):void 
		{
			st = e.soundTransform;
			if (sc) sc.soundTransform = st;
		}
		
		public function get position():Number { return sc ? sc.position : _pos; }
		
		public function play(pos:Number = 0):void
		{
			if (!sound) return;
			
			pause();
			
			sc = sound.play(pos, 0, st);
			if(sc) sc.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
		}
		
		public function stop():void
		{
			pause();
			_pos = 0;
		}
		
		public function pause():void
		{
			if (!sc) return;
			
			_pos = sc.position;
			sc.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
			sc.stop();
			sc = null;
		}
		
		public function resume():void
		{
			play(_pos);
		}
		
		public function load(url:String):void
		{
			sound.load(new URLRequest(url));
		}
		
		private function soundCompleteHandler(e:Event):void 
		{
			
		}
	}
}